package com.block.merchant.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.block.account.enums.AccountTypeEnum;
import com.block.account.enums.ActionTypeEnum;
import com.block.account.model.dto.ChangeAmountDTO;
import com.block.account.model.result.ChangeAmountResult;
import com.block.account.service.IAccountHandleService;
import com.block.common.constant.AppRspCodeConstant;
import com.block.common.vo.ResponseVo;
import com.block.db.entity.MerchantPayOrderInfo;
import com.block.db.mapper.MerchantPayOrderInfoMapper;
import com.block.merchant.enums.OrderStatusEnum;
import com.block.merchant.enums.OrderTypeEnum;
import com.block.merchant.service.IMerchantPayOrderInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.math.BigDecimal;

/**
 * <p>
 * 商户支付订单信息 服务实现类
 * </p>
 *
 * @author 
 * @since 2024-04-19
 */
@Service
public class MerchantPayOrderInfoServiceImpl extends ServiceImpl<MerchantPayOrderInfoMapper, MerchantPayOrderInfo> implements IMerchantPayOrderInfoService {

    @Autowired
    IAccountHandleService accountHandleService;

    @Override
    @Transactional
    public ResponseVo settlePayOrder(String orderNo, String operatorId, String operatorName) {

        MerchantPayOrderInfo payOrderInfo = this.baseMapper.selectById(orderNo);
        if (ObjectUtil.isEmpty(payOrderInfo)){
            return new ResponseVo(AppRspCodeConstant.SYS_EXCEPTION, "订单信息为空");
        }
        if (!StrUtil.equals(payOrderInfo.getOrderType(), OrderTypeEnum.MANUAL.getValue())){
            return new ResponseVo(AppRspCodeConstant.SYS_EXCEPTION, "订单类型错误");
        }
        if (!StrUtil.equals(payOrderInfo.getStatus(), OrderStatusEnum.INIT.getValue())){
            return new ResponseVo(AppRspCodeConstant.SYS_EXCEPTION, "订单状态错误");
        }


        ActionTypeEnum actionTypeEnum = ActionTypeEnum.getEntityByType(payOrderInfo.getActionType());
        // 组装 支付信息
        ChangeAmountDTO changeAmountDTO = new ChangeAmountDTO();
        changeAmountDTO.setAccountTypeEnum(AccountTypeEnum.MERCHANT);
        changeAmountDTO.setActionTypeEnum(actionTypeEnum);
        changeAmountDTO.setChangeAmount(payOrderInfo.getCostAmount());
        changeAmountDTO.setChangeZhiCoinAmount(payOrderInfo.getZhiCoinAmount());
        changeAmountDTO.setAccountId(payOrderInfo.getInfoId());
        String dataInfo = StrUtil.format("支付订单号：{}", payOrderInfo.getOrderNo());
        changeAmountDTO.setDataInfo(dataInfo);
        String remarkInfo = StrUtil.format("{}[{}] {} {} 直币",
                payOrderInfo.getInfoName(), payOrderInfo.getInfoId(), actionTypeEnum.getName(), payOrderInfo.getZhiCoinAmount());
        changeAmountDTO.setRemarkInfo(remarkInfo);
        changeAmountDTO.setOperatorId(operatorId);
        changeAmountDTO.setOperatorName(operatorName);
        changeAmountDTO.setMainAccountId(payOrderInfo.getMainAccountId());

        MerchantPayOrderInfo updateEntity = new MerchantPayOrderInfo();
        updateEntity.setOrderNo(payOrderInfo.getOrderNo());

        ChangeAmountResult changeAmountResult = accountHandleService.changeAmountHandle(changeAmountDTO);
        if (changeAmountResult.isSuccess()){
            // 订单支付成功
            updateEntity.setStatus(OrderStatusEnum.SUCCESS.getValue());
            updateEntity.setRemarkInfo("操作成功");
        }else{
            updateEntity.setStatus(OrderStatusEnum.FAILED.getValue());
            updateEntity.setRemarkInfo("入账失败："+changeAmountResult.getMessage());
        }
        boolean b = this.updateById(updateEntity);
        if (!b){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseVo(AppRspCodeConstant.SYS_EXCEPTION, "订单结算失败");
        }

        return new ResponseVo(AppRspCodeConstant.SUCCESS);
    }
}
